﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Service;
using System.Collections;
using Cashier.utils;
using Cashier_Model;

namespace Cashier.payments
{
    public partial class PaymentsAddForm : Form
    {
        private MainForm mainform;
        private PaymentsControl paymentsControl;
        private PaymentsService paymentsService;
        private Payments payments;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="paymentsControl"></param>
        /// <param name="id"></param>
        public PaymentsAddForm(MainForm mainform, PaymentsControl paymentsControl, long id)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.paymentsControl = paymentsControl;
            this.paymentsService = new PaymentsService();
            this.comboBox_paymentssortid.BindData(mainform);

            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            this.InitialFormData(id);
        }

        #region 功能方法实现

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        /// <param name="id"></param>
        private void InitialFormData(long id)
        {
            if (id > 0)
            {
                try
                {
                    payments = paymentsService.GetPayments(id);
                    if (payments != null)
                        FormUtil.InitialFormData(payments, this.groupBox1, 1);
                }
                catch (Exception ex)
                {
                    LogsUtil.Record(ex);
                }
            }
        }

        /// <summary>
        /// 保存收支明细
        /// </summary>
        private void SavePayments()
        {
            ArrayList list = new ArrayList();
            FormUtil.GetValidateControlsNotPassed(this.groupBox1, list, 1);
            if (list.Count > 0)
                return;

            if (comboBox_paymentssortid.SelectedIndex == 0)
            {
                MessageBox.Show("请选择收支分类！");
                return;
            }

            if (payments == null)
                payments = new Payments();
            FormUtil.GetFormData(payments, this.groupBox1, 1);

            try
            {
                bool issuccess = false;
                if (payments.Id > 0)
                {
                    int flag = this.paymentsService.UpdatePayments(payments);
                    if (flag > 0)
                        issuccess = true;
                }
                else
                {
                    payments.EnterpriseNo = mainform.Admin.EnterpriseNo;
                    payments.StoreNo = mainform.Admin.StoreNo;
                    payments.AdminUsername = mainform.Admin.Username;
                    payments.AdminName = mainform.Admin.Name;
                    payments.PaymentsTime = DateTime.Now;
                    payments.Createtime = DateTime.Now;
                    long id = this.paymentsService.InsertPayments(payments);
                    if (id > 0)
                    {
                        payments.Id = id;
                        issuccess = true;
                    }
                }

                if (issuccess)
                {
                    DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        payments = null;
                        FormUtil.ClearFormData(this, 1);
                    }
                    else
                    {
                        this.Close();
                    }
                    paymentsControl.RefreshDataGridViewData();
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

        #region 重写函数

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
            return b;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            this.SavePayments();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}
